package ciyuan.Block.Machine.yjcxj;

import ciyuan.Gui.ContainerRegistry;
import ciyuan.Item.ItemRegisterManager;
import ciyuan.Utils.TileEntityRegisterManager;
import net.minecraft.block.BlockState;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.Inventory;
import net.minecraft.inventory.container.Container;
import net.minecraft.inventory.container.INamedContainerProvider;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.tileentity.ITickableTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;

import javax.annotation.Nullable;

/**
 * @author 150149
 */
public class YjcxjTileEntity extends TileEntity implements ITickableTileEntity, INamedContainerProvider {

    private final Inventory inventory = new Inventory(5);
    public ciyuan.Block.Machine.yjcxj.ContainerProgress containerProgress = new ContainerProgress();

    public YjcxjTileEntity() {
        super(TileEntityRegisterManager.yjcxj.get());
    }

    @Override
    public ITextComponent getDisplayName() {
        return new TranslationTextComponent("ciyuan.container.dsc20");
    }

    @Nullable
    @Override
    public Container createMenu(int sycID, PlayerInventory inventory, PlayerEntity player) {
        return new YjcxjContainer(ContainerRegistry.yjcxjContainer.get(), sycID, inventory, getBlockPos(), containerProgress);
    }

    @Override
    public void load(BlockState Yjcxjate, CompoundNBT nbt) {
        if (!hasLevel()) {
            this.inventory.setItem(0,ItemStack.of(nbt.getCompound("item")));
            this.inventory.setItem(1,ItemStack.of(nbt.getCompound("item2")));
            this.inventory.setItem(2,ItemStack.of(nbt.getCompound("item3")));
            this.inventory.setItem(3,ItemStack.of(nbt.getCompound("item4")));
            this.inventory.setItem(4,ItemStack.of(nbt.getCompound("item5")));
            this.containerProgress.set(0, nbt.getInt("water"));
        }
        super.load(Yjcxjate, nbt);
    }

    @Override
    public CompoundNBT save(CompoundNBT compound) {
        ItemStack itemYjcxjack = this.inventory.getItem(0).copy();
        ItemStack itemYjcxjack2 = this.inventory.getItem(1).copy();
        ItemStack itemYjcxjack3 = this.inventory.getItem(2).copy();
        ItemStack itemYjcxjack4 = this.inventory.getItem(3).copy();
        ItemStack itemYjcxjack5 = this.inventory.getItem(4).copy();
        compound.put("item", itemYjcxjack.serializeNBT());
        compound.put("item2", itemYjcxjack2.serializeNBT());
        compound.put("item3", itemYjcxjack3.serializeNBT());
        compound.put("item4", itemYjcxjack4.serializeNBT());
        compound.put("item5", itemYjcxjack5.serializeNBT());
        compound.putInt("water",this.containerProgress.get(0) );
        return super.save(compound);
    }

    public Inventory getInventory() {
        return inventory;
    }

    @Override
    public void tick() {
        if (!this.inventory.getItem(0).isEmpty() && !this.inventory.getItem(1).isEmpty()) {
            if (!this.inventory.getItem(2).isEmpty() && !this.inventory.getItem(3).isEmpty()) {
                if (this.inventory.getItem(0).getItem().getDescriptionId().equals(ItemRegisterManager.steelstick.get().getDescriptionId()) && this.inventory.getItem(0).getCount()>=5) {
                    if (this.inventory.getItem(1).getItem().getDescriptionId().equals(ItemRegisterManager.Silicon.get().getDescriptionId()) && this.inventory.getItem(0).getCount()>=5) {
                        if (this.inventory.getItem(2).getItem().getDescriptionId().equals(ItemRegisterManager.sulphuricacid.get().getDescriptionId())) {
                            if (this.inventory.getItem(3).getItem().getDescriptionId().equals(ItemRegisterManager.tin.get().getDescriptionId()) && this.inventory.getItem(0).getCount()>=5) {
                                if (this.containerProgress.get(0)>=800 && this.inventory.canAddItem(new ItemStack(ItemRegisterManager.transistor.get()))) {
                                    this.inventory.getItem(0).setCount(this.inventory.getItem(0).getCount()-5);
                                    this.inventory.getItem(1).setCount(this.inventory.getItem(1).getCount()-5);
                                    this.inventory.getItem(2).setCount(this.inventory.getItem(2).getCount()-1);
                                    this.inventory.getItem(3).setCount(this.inventory.getItem(3).getCount()-5);
                                    this.inventory.addItem(new ItemStack(ItemRegisterManager.transistor.get()));
                                    this.containerProgress.set(0,0);
                                } else if (this.containerProgress.get(0)<800){
                                    this.containerProgress.set(0,this.containerProgress.get(0)+1);
                                }
                            }
                        }
                    }
                }
            }
        }
    }



}
